package com.foreverwin.jzymes.report.fqc.service.impl;

import com.foreverwin.jzymes.framework.util.DateUtil;
import com.foreverwin.jzymes.production.fqc.domain.FqcCheck;
import com.foreverwin.jzymes.production.fqc.domain.FqcCheckBox;
import com.foreverwin.jzymes.production.fqc.domain.FqcCheckNc;
import com.foreverwin.jzymes.report.fqc.mapper.FqcCheckReportMapper;
import com.foreverwin.jzymes.report.fqc.service.IFqcCheckReportService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class FqcCheckReportServiceImpl implements IFqcCheckReportService {
    @Autowired
    FqcCheckReportMapper fqcCheckReportMapper;

    /**
     * FQC检验报表
     *
     * @param jsonMap
     * @return
     */
    @Override
    public List<FqcCheck> findFqcCheckByReport(Map<String, Object> jsonMap) {
        String site = (String)jsonMap.get("site");
        String itemBo = (String)jsonMap.get("itemBo");
        Date startDate = null;
        Date endDate = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if ( (String)jsonMap.get("startDate") != "" ){
            try {
                startDate = simpleDateFormat.parse( (String)jsonMap.get("startDate") );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if ( (String)jsonMap.get("endDate") != "" ){
            try {
                endDate = simpleDateFormat.parse( (String)jsonMap.get("endDate") );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        List<FqcCheck> fqcChecks = fqcCheckReportMapper.selectFqcCheckByReport( site , itemBo , startDate ,endDate );


        return fqcChecks;
    }
    /**
     * FQCBox
     *
     * @param jsonMap
     * @return
     */
    @Override
    public  List<Map<String , Object> > findFqcCheckBoxByReport(Map<String , Object> jsonMap) {
       String site =  (String)jsonMap.get("site");
       String handle = (String)jsonMap.get("handle");

       Date createdDateTime = null;
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       if ( (String)jsonMap.get("createdDateTime") != ""){
           try {
               createdDateTime = simpleDateFormat.parse((String)jsonMap.get("createdDateTime"));
           } catch (ParseException e) {
               e.printStackTrace();
           }
       }

      List<FqcCheckBox> fqcCheckBoxes = fqcCheckReportMapper.selectFqcCheckBoxByReport(  handle );
      List<Map<String , Object> >  list = new ArrayList<>();
      for ( FqcCheckBox fqcCheckBox : fqcCheckBoxes ){
          Map<String,Object> map = new HashMap<>();
          map.put("qty" , fqcCheckBox.getQty());
          map.put("containerNumber",fqcCheckBox.getContainerNumber());
          map.put( "createdDateTime" , createdDateTime );

          list.add( map );
      }
        return list;
    }
    /**
     * FQCNC
     *
     * @param jsonMap
     * @return
     */
    @Override
    public List<Map<String, Object>> findFqcCheckNCByReport(Map<String, Object> jsonMap) {

        String handle = (String)jsonMap.get("handle");

        Date createdDateTime = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if ( (String)jsonMap.get("createdDateTime") != ""){
            try {
                createdDateTime = simpleDateFormat.parse((String)jsonMap.get("createdDateTime"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        List< FqcCheckNc > fqcCheckncBoxes = fqcCheckReportMapper.selectFqcCheckNCByReport( handle );
        List<Map<String , Object> >  list = new ArrayList<>();
        for ( FqcCheckNc fqcCheckNc : fqcCheckncBoxes ){
            Map<String,Object> map = new HashMap<>();
            map.put("ncCode" , fqcCheckNc.getNcCode());
            map.put("reworkQty" , fqcCheckNc.getReworkQty());
            map.put("scrapQty" , fqcCheckNc.getScrapQty());
            map.put( "createdDateTime" , createdDateTime );

            list.add( map );
        }
        return list;

    }
}
